package com.ehsunbehravesh.mypasswords;

import com.ehsunbehravesh.mypasswords.gui.AboutDialog;
import com.ehsunbehravesh.mypasswords.gui.MainFrame;
import com.ehsunbehravesh.mypasswords.gui.PasswordGeneratorDialog;
import com.ehsunbehravesh.mypasswords.resource.ResourceLoader;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class MyPasswords {

  private static JDialog splash;

  public static void main(String args[]) {
    splash = new JDialog();
    splash.setSize(new Dimension(400, 270));
    splash.setUndecorated(true);
    Utils.setCenterOfScreen(splash);
    splash.setVisible(true);
    ImageIcon img = new ImageIcon(ResourceLoader.class.getResource("splash.png"));
    JPanel pnlMain = (JPanel) splash.getContentPane();
    pnlMain.setBackground(Color.WHITE);
    pnlMain.add(new JLabel(img), BorderLayout.CENTER);
    splash.setContentPane(pnlMain);

    java.awt.EventQueue.invokeLater(new Runnable()        {

      @Override
      public void run() {
        setLookAndFeel();
        AboutDialog aboutDialog = new AboutDialog(null, false);
        PasswordGeneratorDialog passwordGeneratorDialog = new PasswordGeneratorDialog(null, false);

        new MainFrame(aboutDialog, passwordGeneratorDialog).setVisible(true);
        splash.setVisible(false);
      }

      private void setLookAndFeel() {
        try {
          File file = new File(Utils.getPathOfJar() + "settings.properties");
          Boolean systemLookAndFeel = false;
          if (file.exists()) {
            FileInputStream fileInputStream = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(fileInputStream);

            systemLookAndFeel = Boolean.parseBoolean(properties.getProperty("systemLookAndFeel"));
          }
          if (!systemLookAndFeel) {
            boolean nimbusSupport = false;
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
              if ("Nimbus".equals(info.getName())) {
                nimbusSupport = true;
                UIManager.setLookAndFeel(info.getClassName());
                break;
              }
            }
            if (!nimbusSupport) {
              JOptionPane.showMessageDialog(null, "Nimbus LookAndFeel is not supported by your system!", "Warning", JOptionPane.INFORMATION_MESSAGE);
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
          } else {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          }
        } catch (Exception ex) {
          Logger.log(ex.getMessage());
        }
      }
    });
  }
}
